﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Genealogy_Builder
{
    public partial class PersonDialog : Form
    {
        public Person newPerson;
        List<Place> placeList;

        bool pass = true;

        public PersonDialog(int id, List<Place> places)
        {
            InitializeComponent();
            txtPID.Text = Person.ID.ToString(); ;
            placeList = new List<Place>();
            foreach (Place p in places)
            {
                placeList.Add(p);
            }
            cmbFrom.DataSource = places;
            cmbLivesIn.DataSource = placeList;
            newPerson = null;
        }

        public PersonDialog(Person init, List<Place> places)
        {
            InitializeComponent();
            txtPID.Text = init.pid.ToString();
            txtName.Text = init.name;
            
            placeList = new List<Place>();
            int index = 0;
            int fromIndex = -1;
            int livesInIndex = -1;
            foreach (Place p in places)
            {
                placeList.Add(p);
                if (init.from != null && p.place_id == init.from.place_id)
                    fromIndex = index;
                if (init.lives_in != null && p.place_id == init.lives_in.place_id)
                    livesInIndex = index;
                index++;
            }
            cmbFrom.DataSource = places;
            if (fromIndex != -1)
                cmbFrom.SelectedIndex = fromIndex;
            else
                cmbFrom.Text = "";
            cmbLivesIn.DataSource = placeList;
            if (livesInIndex != -1)
                cmbLivesIn.SelectedIndex = livesInIndex;
            else
                cmbLivesIn.Text = "";

            dateBirth.Value = init.birth;
            if (init.death == DateTime.MinValue)
                chBoxAlive.Checked = true;
            else
            {
                chBoxAlive.Checked = false;
                dateDeath.Value = init.death;
                cmbLivesIn.Enabled = false;
                cmbLivesIn.Text = "";
            }

            newPerson = init;
        }

        public bool AddPerson()
        {
            this.ShowDialog();
            return pass;
        }

        private void chBoxAlive_CheckedChanged(object sender, EventArgs e)
        {
            dateDeath.Enabled = !(chBoxAlive.Checked);
            cmbLivesIn.Enabled = chBoxAlive.Checked;
            if (chBoxAlive.Checked == false)
                cmbLivesIn.Text = "";
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtName.Text == "")
            {
                MessageBox.Show("Please provide a name.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (newPerson == null)
                {
                    newPerson = new Person(txtName.Text, (Place)cmbLivesIn.SelectedItem, (Place)cmbFrom.SelectedItem, dateBirth.Value, (chBoxAlive.Checked == false) ? dateDeath.Value : DateTime.MinValue);
                }
                else
                {
                    newPerson.name = txtName.Text;
                    newPerson.lives_in = (Place)cmbLivesIn.SelectedItem;
                    newPerson.from = (Place)cmbFrom.SelectedItem;
                    newPerson.birth = dateBirth.Value;
                    if (chBoxAlive.Checked)
                        newPerson.death = dateDeath.Value;
                    else
                        newPerson.death = DateTime.MinValue;
                }
                this.Close();
                
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            pass = false;
            this.Close();
        }

    }
}
